import {
  Get,
  Post,
  JsonPost,
  API_URL
} from "../../utils/request.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoData: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  // 更换头像
  changeImg(){
    let that=this
    wx.chooseImage({
      count: 1,
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        wx.uploadFile({
          url: API_URL+'user/image', //仅为示例，非真实的接口地址
          filePath: res.tempFilePaths[0],
          name: 'upload',
          header	:{'X-Access-Token':wx.getStorageSync('token')},
          formData: {
            'upload': res.tempFilePaths[0]
          },
          success (Ies){
            let img=(JSON.parse(Ies.data)).data
            JsonPost('user/update-avatar',{'avatar':img}).then(Ues=>{
              if (Ues.code==0) {
                wx.showToast({
                  title: '更改成功',
                  icon: 'none',
                  duration: 2000
                })
                that.getInfo()
              }
            })
          }
        })
      }
    })
  },
  // 更换姓名
  updatName(e){
    let that=this
    wx.showModal({
      title: '提示',
      content: '是否更改昵称',
      success (res) {
        if (res.confirm) {
          JsonPost('user/update-nickname',{'username':e.detail.value}).then(res=>{
            wx.showToast({
              title: '更改成功',
              icon: 'none',
              duration: 2000
            })
            that.getInfo()
          })
        }
      }
    })
  },

  bindDateChange(e) {
    let that=this
    wx.showModal({
      title: '提示',
      content: '是否更改出生日期',
      success (res) {
        if (res.confirm) {
          JsonPost('user/update-birthday',{'birthday':e.detail.value}).then(res=>{
            wx.showToast({
              title: '更改成功',
              icon: 'none',
              duration: 2000
            })
            that.getInfo()
          })
        }
      }
    })
  },
  sexChange(e){
    let that=this
    JsonPost('user/update-gender',{'sex':e.detail.value}).then(res=>{
      that.getInfo()
    })
  },


  // 跳转地址管理
  addressList: function (e) {
  wx.navigateTo({
    url: '/pages/adress/adress',
  })
  },

  // 跳转修改手机号
  goRevisePhone: function (e) {
  wx.navigateTo({
    url:'/pages/revisePhone/revisePhone?status='+(this.data.infoData.mobile?1:2),
  })
  },

  getInfo(){
    JsonPost('user/info').then(res=>{
      this.setData({
        infoData:res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getInfo()
  },
})